package com.lap.framework.generator.shared.tool;

import com.lap.framework.common.tool.Named;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

@Slf4j
@UtilityClass
public class PackUtil {

  private static final String EMPTY = "";
  private static final String PACKAGE_SEPARATOR = ".";

  /** 包转换到路径 */
  public static String pack2Path(String... packageNames) {
    if (ArrayUtils.isEmpty(packageNames)) return EMPTY;

    StringBuilder result = new StringBuilder();
    for (String pkg : packageNames) {
      if (StringUtils.isNotBlank(pkg)) {
        // 去除空格并转换
        String path = Named.periods2Path(pkg.trim());
        result.append(path);
      }
    }
    return result.toString();
  }

  /** 多个包拼接 */
  public static String pack(String... packageNames) {
    if (ArrayUtils.isEmpty(packageNames)) return EMPTY;

    return Stream.of(packageNames)
        .filter(StringUtils::isNotBlank)
        .map(String::trim)
        // 去除开头和结尾的点号
        .map(pkg -> StringUtils.strip(pkg, PACKAGE_SEPARATOR))
        .filter(StringUtils::isNotBlank)
        .collect(Collectors.joining(PACKAGE_SEPARATOR));
  }
}
